package com.huawei.esdk.tp.northcommu.cxf.professional;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.platform.common.utils.DateUtils;
import com.huawei.esdk.platform.common.utils.DurationUtils;
import com.huawei.esdk.platform.common.utils.ListUtils;
import com.huawei.esdk.tp.business.professional.mcumgr.MCUManagerService;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.tp.domain.model.bean.MCUInfoList;
import com.huawei.esdk.tp.domain.model.bean.MCUResourceList;
import com.huawei.esdk.tp.northcommu.cxf.professional.convert.TPProfessionalMCUMgrConvert;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.QueryMCUFutureResourceEx;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.QueryMCUFutureResourceExResponse;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.QueryMCUInfoEx;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.QueryMCUInfoExResponse;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.TPProfessionalMCUMgr;

public class TPProfessionalMCUMgrImpl implements TPProfessionalMCUMgr
{
    private MCUManagerService mcuManagerService;
    
    private static final Logger LOGGER = Logger.getLogger(TPProfessionalMCUMgrImpl.class);
    
    private TPProfessionalMCUMgrConvert tpProfessionalMCUMgrConvertService;
    
    @Override
    public QueryMCUFutureResourceExResponse queryMCUFutureResourceEx(QueryMCUFutureResourceEx parameters)
    {
        QueryMCUFutureResourceExResponse response = new QueryMCUFutureResourceExResponse();
        
        // DTS2015012805861 参数duration为空时，应该查询失败，返回错误码；实际情况是查询成功，返回资源占用情况
        // 在北向加上必填参数校验，如果必填参数为空，则返回错误码
        if (null == parameters || null == parameters.getBeginTime() || null == parameters.getDuration()
            || null == parameters.getMcus())
        {
            response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
            return response;
        }
        
        Integer duration = DurationUtils.duration2int(parameters.getDuration());
        Date beginTime = DateUtils.toDate(parameters.getBeginTime());
        List<Integer> mcus = parameters.getMcus();
        
        //DTS2015012603196 mcus为空，查未来时间MCU的资源占用情况，返回2130000010错误
        //在北向加上判断，如果必填参数为空，则返回错误码
        if (null == duration || null == beginTime || mcus.isEmpty()
            || ListUtils.isEmptyListForInt(mcus))
        {
            response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
            return response;
        }
        
        try
        {
            // 查询MCU信息列表
            SDKResult<MCUResourceList> result = mcuManagerService.queryMCUFutureResourceEx(duration, beginTime, mcus);
            
            response = tpProfessionalMCUMgrConvertService.queryMCUFutureResourceExModal2Soap(result);
        }
        catch (SDKException ex)
        {
            LOGGER.error("", ex);
            response.setResultCode((int)ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("", e);
            response.setResultCode((int)ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        
        return response;
    }
    
    @Override
    public QueryMCUInfoExResponse queryMCUInfoEx(QueryMCUInfoEx parameters)
    {
        QueryMCUInfoExResponse response = new QueryMCUInfoExResponse();
        
        // DTS2015012305074 排序方式sort为3或者为空，itemIndex为空或9，查询MCU信息，返回2130000010错误
        // 必填参数校验
        if (null == parameters || null == parameters.getQueryConfig()
            || null == parameters.getQueryConfig().getPageParam()
            || null == parameters.getQueryConfig().getPageParam().getCurrentPage()
            || null == parameters.getQueryConfig().getPageParam().getNumberPerPage())
        {
            response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
            return response;
        }
        
        try
        {
            // 查询MCU信息列表
            SDKResult<MCUInfoList> result =
                mcuManagerService.queryMCUInfoEx(tpProfessionalMCUMgrConvertService.queryMCUInfoExSoap2Modal(parameters));
            
            response = tpProfessionalMCUMgrConvertService.queryMCUInfoExModal2Soap(result);
        }
        catch (SDKException ex)
        {
            LOGGER.error("", ex);
            response.setResultCode((int)ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("", e);
            response.setResultCode((int)ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        
        return response;
    }
    
    public MCUManagerService getMcuManagerService()
    {
        return mcuManagerService;
    }
    
    public void setMcuManagerService(MCUManagerService mcuManagerService)
    {
        this.mcuManagerService = mcuManagerService;
    }
    
    public TPProfessionalMCUMgrConvert getTpProfessionalMCUMgrConvertService()
    {
        return tpProfessionalMCUMgrConvertService;
    }
    
    public void setTpProfessionalMCUMgrConvertService(TPProfessionalMCUMgrConvert tpProfessionalMCUMgrConvertService)
    {
        this.tpProfessionalMCUMgrConvertService = tpProfessionalMCUMgrConvertService;
    }
    
}
